# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 -------------------------------------------------
    File Name:     caselog.py
    Description:   
 -------------------------------------------------
 """

from hellchin_lib import LoggerManager

# 初始化日志工具
logger = LoggerManager().configure()


class CaseRunLog:
    """用户运行测试用例的记录"""

    def save_log(self, message, level):
        if not hasattr(self, 'log_data'):
            setattr(self, 'log_data', [])

        output_string = f"【{level}】".ljust(10) + "|   {}".format(message)

        # 根据日志级别，生成不同的日志信息
        if level in ['DEBUG', 'EXCEPT']:
            info = output_string
        elif level == "WARNING":
            # logger.warning(output_string)
            info = output_string
        elif level == "INFO":
            # logger.info(output_string)
            info = output_string
        else:
            info = output_string

        getattr(self, 'log_data').append(
            (level, info)
        )

        if level == 'DEBUG':
            logger.info((level, info))
        else:
            getattr(logger, level.lower())(info)

    def info_log(self, *args):
        message = ''.join(args)
        self.save_log(message=message, level='INFO')

    def warning_log(self, *args):
        message = ''.join(args)
        self.save_log(message, 'WARNING')

    def error_log(self, *args):
        message = ''.join(args)
        self.save_log(message, 'ERROR')

    def exception_log(self, *args):
        message = ''.join(args)
        self.save_log(message, 'EXCEPT')

    def critical_log(self, *args):
        message = ''.join(args)
        self.save_log(message, 'CRITICAL')

    def debug_log(self, *args):
        """调试日志"""
        message = ''.join(args)
        self.save_log(message, 'DEBUG')

    def script_log(self, *args):
        """脚本日志"""
        message = ''.join(args)
        self.save_log(message, 'SCRIPT')

    def print_log(self, *args):
        args = [str(i) for i in args]
        message = ' '.join(args)
        if not hasattr(self, 'log_data'):
            setattr(self, 'log_data', [])
        getattr(self, 'log_data').append(('DEBUG', message))
        print(('PRINT', message))


if __name__ == '__main__':
    log = CaseRunLog()
    log.info_log("正常信息")
    log.warning_log("警告")
    log.error_log("错误")
    log.debug_log("日志", '123')
    log.print_log("打印")
